﻿package frEngine.loaders.resource.info
{
    import baseEngine.system.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.gengine.resource.mutiThread.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.animateControler.particleControler.*;
    import frEngine.event.*;
    import frEngine.loaders.*;
    import frEngine.loaders.particleSub.*;
    import frEngine.multiThread.*;

    public class ParticleInfo extends Info3dBase
    {
        private var _particleFileData:ParticleFileData;
        private var _particleParams:ParticleParams;
        private var _usedThread:Boolean;

        public function ParticleInfo(param1:Object)
        {
            this._particleParams = new ParticleParams(false);
            super(param1);
            return;
        }// end function

        public function get particleFileData() : ParticleFileData
        {
            return this._particleFileData;
        }// end function

        override public function dispose() : void
        {
            if (this.isLoaded)
            {
                super.dispose();
                this._particleParams.dispose();
            }
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            var fileParams:Object;
            var str:String;
            var needTestResult:Array;
            var data:Object;
            var vo:Object;
            var particleParser:ParticleParser;
            var errorInfo:String;
            var value:* = param1;
            super.data = value;
            _byteArray.position = 0;
            _byteArray.endian = Endian.LITTLE_ENDIAN;
            var versionValue:* = _byteArray.readShort();
            var reRead:Boolean;
            this._usedThread = Global.threadManager != null && !Global3D.isOpenGL;
            if (versionValue == 2826)
            {
                if (!this._usedThread)
                {
                    _byteArray.position = 0;
                    fileParams = _byteArray.readObject();
                }
            }
            else
            {
                try
                {
                    _byteArray.position = 0;
                    str = String(_byteArray);
                    fileParams = JSON.parse(str);
                    needTestResult = ParticleInfo.replaceSourceUrl(fileParams);
                    if (!needTestResult)
                    {
                        return;
                    }
                }
                catch (e:Error)
                {
                    Log.debugLog.error("无效的 JSON 解析输入:" + this.name);
                    return;
                }
            }
            if (!this._particleFileData)
            {
                this._particleFileData = new ParticleFileData();
                this._particleFileData.targetInfo = this;
            }
            else
            {
                this._particleFileData.dispose();
            }
            if (this._usedThread)
            {
                data = fileParams == null ? (_byteArray) : (fileParams);
                vo;
                F3DThreadManager.instance.sendMessageToSub(vo, this.initDataComplete);
            }
            else
            {
                this._particleParams._useGpu = Global3D.useGpuParticle;
                particleParser = new ParticleParser(false);
                errorInfo = particleParser.parser(this._particleParams, fileParams);
                if (errorInfo == null)
                {
                    this._particleParams.startBuild(this._particleFileData.builderSurfaceImp);
                }
            }
            return;
        }// end function

        private function initDataComplete(param1:ParticleParams) : void
        {
            if (this._particleFileData && param1.parserSuccess && !this.isDispose)
            {
                this._particleFileData.builderSurfaceImp(param1);
            }
            return;
        }// end function

        override public function proceedParsing() : void
        {
            if (proceedParsed || !_byteArray)
            {
                return;
            }
            if (this._usedThread)
            {
                proceedParsed = true;
                FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.PARSE_RESOURCE_FINISH);
            }
            else if (!this._particleParams.hasCompleted)
            {
                this._particleParams.nextStepParser();
            }
            else
            {
                proceedParsed = true;
                FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.PARSE_RESOURCE_FINISH);
            }
            return;
        }// end function

        public static function replaceSourceUrl(param1:Object) : Array
        {
            var _loc_3:String = null;
            var _loc_4:ResourceInfo = null;
            var _loc_2:* = new Array();
            if (param1.particleType == EngineConstName.particleType2)
            {
                _loc_3 = param1.particleMesh;
                if (_loc_3 && _loc_3 != EngineConstName.defaultNullStringFlag)
                {
                    _loc_4 = ResourceManager.getInfoByName(_loc_3);
                    if (_loc_4)
                    {
                        param1.particleMesh = _loc_4.path;
                        _loc_2.push(_loc_4.path);
                    }
                    else
                    {
                        Log.error("特效资源不存在 ->" + _loc_3);
                        return null;
                    }
                }
                else
                {
                    Log.error("特效资源不存在 -> fileParams.particleMesh");
                    return null;
                }
            }
            if (param1.formation == EngineConstName.emitterShapeChoose6)
            {
                _loc_3 = param1.emitterShapeChoose;
                if (_loc_3 && _loc_3 != EngineConstName.defaultNullStringFlag)
                {
                    _loc_4 = ResourceManager.getInfoByName(_loc_3);
                    if (_loc_4)
                    {
                        param1.emitterShapeChoose = _loc_4.path;
                        _loc_2.push(_loc_4.path);
                    }
                    else
                    {
                        Log.error("特效资源不存在 ->" + _loc_3);
                        return null;
                    }
                }
                else
                {
                    Log.error("特效资源不存在 -> fileParams.emitterShapeChoose");
                    return null;
                }
            }
            if (param1.emitPlace == EmitterPosType.TextureRGB)
            {
                _loc_3 = param1.emitterTextureChoose;
                if (_loc_3 && _loc_3 != EngineConstName.defaultNullStringFlag)
                {
                    _loc_4 = ResourceManager.getInfoByName(_loc_3);
                    if (_loc_4)
                    {
                        param1.emitterTextureChoose = _loc_4.path;
                        _loc_2.push(_loc_4.path);
                    }
                    else
                    {
                        Log.error("特效资源不存在 ->" + _loc_3);
                        return null;
                    }
                }
                else
                {
                    Log.error("特效资源不存在 -> fileParams.emitterTextureChoose");
                    return null;
                }
            }
            return _loc_2;
        }// end function

    }
}
